package xyz.prob.kun.base.security.handler;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import xyz.prob.kun.common.enums.HttpStatusEnum;
import xyz.prob.kun.common.model.AjaxResult;
import xyz.prob.kun.common.util.JsonUtil;
import xyz.prob.kun.common.util.ServletUtils;

import java.io.Serial;
import java.io.Serializable;

@Component
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint, Serializable
{
    @Serial
    private static final long serialVersionUID = -8970718410437077606L;

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException e) {
        ServletUtils.renderString(response, JsonUtil.toJSONString(AjaxResult.createOf(HttpStatusEnum.UNAUTHORIZED)));
    }
}
